using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using JigLibX.Vehicles;
using JigLibX.Collision;
using System.Xml;
using PloobsEngine.Input;
using PloobsEngine.Modelo;

namespace PloobsEngine.Physics.XJigLib
{
    public class CarObject : JigLibXObject
    {

     private Car car;
        

    public CarObject(Vector3 position) : this(true, true, 30.0f, 5.0f, 4.7f, 5.0f, 0.20f, 0.4f, 0.05f, 0.45f, 0.3f, 1, 520.0f, 9.8f,position,Matrix.Identity)       
        {            
        }

    public CarObject(bool FWDrive,
                       bool RWDrive,
                       float maxSteerAngle,
                       float steerRate,
                       float wheelSideFriction,
                       float wheelFwdFriction,
                       float wheelTravel,
                       float wheelRadius,
                       float wheelZOffset,
                       float wheelRestingFrac,
                       float wheelDampingFrac,
                       int wheelNumRays,
                       float driveTorque,
                       float gravity , Vector3 position ,Matrix orientation)
                    {
            car = new Car(FWDrive, RWDrive, maxSteerAngle, steerRate,
                wheelSideFriction, wheelFwdFriction, wheelTravel, wheelRadius,
                wheelZOffset, wheelRestingFrac, wheelDampingFrac,
                wheelNumRays, driveTorque, gravity);

            this.Body = car.Chassis.Body;
            this.CollisionSkin = car.Chassis.Skin;
            
            this.Car.EnableCar();
            this.Car.Chassis.Body.MoveTo(position, orientation);
            this.Car.Chassis.Body.AllowFreezing = false;
            SetCarMass(500.0f);

             }

    
    public Car Car
        {
            get { return this.car; }
        }

    private void SetCarMass(float mass)
        {
            Body.Mass = mass;
            Vector3 min, max;
            car.Chassis.GetDims(out min, out max);
            Vector3 sides = max - min;

            float Ixx = (1.0f / 12.0f) * mass * (sides.Y * sides.Y + sides.Z * sides.Z);
            float Iyy = (1.0f / 12.0f) * mass * (sides.X * sides.X + sides.Z * sides.Z);
            float Izz = (1.0f / 12.0f) * mass * (sides.X * sides.X + sides.Y * sides.Y);

            Matrix inertia = Matrix.Identity;
            inertia.M11 = Ixx; inertia.M22 = Iyy; inertia.M33 = Izz;
            car.Chassis.Body.BodyInertia = inertia;
            car.SetupDefaultWheels();            
        }

    public override Matrix WorldMatrix
        {
            get
            {
                return Matrix.CreateScale(Scale) * Body.CollisionSkin.GetPrimitiveLocal(0).Transform.Orientation * Body.Orientation * Matrix.CreateTranslation(Body.Position);                
            }            
        }

    public void MoveDirection(Direcao dir)
        {
            
            if (dir == Direcao.CIMA)
            {
              car.Accelerate = 1.0f;                
            }
            else if (dir == Direcao.BAIXO)
            {
                car.Accelerate = -1.0f;
            }
            else
            {
                car.Accelerate = 0.0f;
            }

            if (dir == Direcao.DIREITA)
            {
                car.Steer = 1.0f;                
            }
            else if (dir == Direcao.ESQUERDA)
            {
                car.Steer = -1.0f;
            }
            else
            {
                car.Steer = 0.0f;
            }

            if (dir == Direcao.VBAIXO)
            {
                car.HBrake = 1.0f;
            }
            else
            {
                car.HBrake = 0.0f;
            }            
        }

    public override void DeSerialize(XmlNode node, EngineStuff engine, IModelo model, Vector3 position, Matrix rotation)
    { 	        
    }
    public override void Serialize(System.Xml.XmlTextWriter writer)
    {        
    }

    public override PhysicObjectTypes PhysicObjectTypes
    {
        get { return PhysicObjectTypes.CAROBJECT; }
    }

        
    }
}
